import time
from lxml import etree
from selenium import  webdriver
import threading
from selenium.common.exceptions import InvalidSelectorException,StaleElementReferenceException,NoSuchElementException

class TaobaoLink(threading.Thread):
    def __init__(self,q,key):
        super().__init__()
        self.driver = webdriver.PhantomJS(executable_path=r'.\browser\phantomjs.exe')
        self.queue = q
        self.key = key
        self.page = 1 #页数

    def run(self):
        self.driver.get('https://s.taobao.com/search?q={}'.format(self.key))
        while self.page <= 100:
            print('正在爬取第{}页链接。。'.format(self.page))
            page_source = self.driver.page_source
            html = etree.HTML(page_source)
            product_links = html.xpath("//a[@class='J_ClickStat']/@href")
            # products = self.driver.find_elements_by_xpath("//a[@class='J_ClickStat']/@href")
            for link in product_links:
                self.queue.put(link)

            while True:

                try:
                    self.driver.find_element_by_xpath("//li[@class='item next']//span[2]").click()
                    break
                except InvalidSelectorException:
                    print('商品链接爬取完成。')
                    return
                except StaleElementReferenceException:
                    print('点击下一页异常，重试。。。')
                    time.sleep(2)
                except NoSuchElementException:
                    print('点击下一页异常，重试。。。')
                    time.sleep(2)
            self.page += 1